import cryptoJs from '../utils/encryptDES.js'
export const request = (params = {}) => {
	//二次封装 uni.request
	return new Promise((resolve, reject) => {
		//请求开始之前 显示loading
		// uni.showLoading({
		// 	title: '加载中....',
		// })
		console.log(params.params, "params.params");
		let data = {}
		switch (params.type) {
			case "apiMixUrlKeyApi":
				data = params.params
				break;
			default:
				data = cryptoJs.encrypt(JSON.stringify(params.params), params.c)
				break;
		}
		uni.request({
			url: params.url,
			data: data || '',
			header: {
				'content-type': 'application/json'
			},
			// timeout: 50000,
			method: params.method || 'GET',
			success: (res) => {
				console.log(res, "==res==");
				if (res.data.data) {
					if (res.data.data.results[0].indexOf("人工智能") != -1)
						uni.navigateTo({
							url: '/pages/minGie/minGie'
						})

				}

				if (res.data.code === 400) {
					console.log('400走了里面')
					uni.showModal({
						title: '提示',
						content: res.data.message,
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				} else if (res.data.code == 406) {
					console.log('0000000000000')
					uni.showModal({
						title: '提示',
						content: '账号涉嫌违规内容，请联系客服解封',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				} else {
					if (res.statusCode == 404) {
						uni.showToast({
							title: '请求失败',
							icon: 'error',
							duration: 2000
						})
					}
				}
				let data = {}
				switch (params.type) {
					case "apiMixUrlApi":
						data = cryptoJs.decrypt(res.data, params.c)
						data = JSON.parse(data)
						break;
					case "apiMixUrlKeyApi":
						data = res
						break;
					case "sslwxbdApi":
						data = cryptoJs.decrypt(res.data.data, params.c)
						data = JSON.parse(data)
						break;
					case "bsqUrl":
						data = cryptoJs.decrypt(res.data.data, params.c)
						data = JSON.parse(data)
						break;
					case "wycUrl":
						data = res
						break;
					default:
						data = cryptoJs.decrypt(res.data, params.c)
						console.log('data==>', data)
						data = JSON.parse(data)
						break;
				}
				resolve(data)
			},
			fail: (err) => {
				uni.showToast({
					title: '失败请重试!',
					icon: 'error',
					duration: 2000
				});
				reject(err)
			},
			complete: () => {
				// if(params.c !== 1005){
				// 	uni.hideLoading()
				// }
				uni.hideLoading()
				uni.stopPullDownRefresh()
			},
		})
	})
}